package jwei.apps.indextank;

import jwei.apps.indextank.adapters.IngredientsCursorAdapter;
import jwei.apps.indextank.db.IngredientDb;
import jwei.apps.indextank.db.IngredientCache.IngredientsCache;
import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SettingsActivity extends ListActivity {

    private IngredientsCursorAdapter ica;

    private final String LOG_TAG = "SettingsActivity";

    private final int DELETE_INGREDIENT = Menu.FIRST;

    private final int ADD_INGREDIENT = 0;

    private Button addIngredient;

    private EditText ingText;

    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_layout);

        addIngredient = (Button) findViewById(R.id.add_my_ingredients);
        addIngredient.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(ADD_INGREDIENT);
            }

        });

        // query for products
        Cursor c = getContentResolver().query(IngredientsCache.CONTENT_URI, null, null, null, null);
        startManagingCursor(c);
        String[] from = new String[] { IngredientsCache.NAME };
        int[] to = new int[] { R.id.restaurant_name, R.id.restaurant_street };

        // set list adapter
        ica = new IngredientsCursorAdapter(SettingsActivity.this, R.layout.ingredient_entry, c, from, to);
        setListAdapter(ica);
        registerForContextMenu(getListView());
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Delete Ingredient?");
        menu.add(0, DELETE_INGREDIENT, 0, "Delete");
    }

    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        if (menuInfo != null) {
            Cursor c = (Cursor) ica.getItem(menuInfo.position);
            switch (item.getItemId()) {
                case DELETE_INGREDIENT:
                    int id = c.getInt(c.getColumnIndex(IngredientsCache._ID));
                    String ingredient = c.getString(c.getColumnIndex(IngredientsCache.NAME));
                    Log.i(LOG_TAG, "DELETING INGREDIENT " + ingredient);

                    IngredientDb idb = IngredientDb.getInstance();
                    idb.removeIngredient(getContentResolver(), id);
                    if (ica != null) {
                        ica.notifyDataSetChanged();
                    }
                    Toast t = Toast.makeText(SettingsActivity.this, "Ingredient removed.", Toast.LENGTH_SHORT);
                    t.show();
                    break;
                default:
                    return super.onContextItemSelected(item);
            }
        }
        return false;
    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case ADD_INGREDIENT:
                dialog = new Dialog(SettingsActivity.this);

                dialog.setContentView(R.layout.add_my_ingredients_dialog);
                dialog.setTitle("Add an Ingredient");

                ingText = (EditText) dialog.findViewById(R.id.add_my_ingredients_text);
                Button submit = (Button) dialog.findViewById(R.id.submit_my_ingredient);
                submit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String ing = ingText.getText().toString();
                        if (ing.equals("")) {
                            Toast t = Toast.makeText(SettingsActivity.this, "Please type an ingredient!",
                                    Toast.LENGTH_SHORT);
                            t.show();
                        } else {
                            IngredientDb idb = IngredientDb.getInstance();
                            idb.addIngredient(getContentResolver(), ing);
                            if (ica != null) {
                                ica.notifyDataSetChanged();
                            }
                            Toast t = Toast.makeText(SettingsActivity.this, "Ingredient added!", Toast.LENGTH_SHORT);
                            t.show();

                            ingText.setText("");
                            dialog.dismiss();
                        }

                    }

                });
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

}
